home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-games-data / gnome_sudoku / timer.py < prev   
Encoding:
Python Source  |  2009-04-14  |  7.4 KB  |  184 lines

  1. # -*- coding: utf-8 -*-
  2. import gtk, gobject, gtk.glade
  3. import os, os.path
  4. import time
  5. from gettext import gettext as _
  6. from gettext import ngettext
  7. from defaults import *
  8.  
  9. def format_time (time, round_at=None, friendly=False):
  10.     """Format a time for display to the user.
  11.  
  12.     If round_at, we round all times to some number of seconds.
  13.  
  14.     If friendly, we don't bother showing the user more than two
  15.     units. i.e. 3 days 2 hours, or 2 minutes 30 seconds, but not 3
  16.     days, 4 hours, 2 minutes and 3 seconds...
  17.     """
  18.     time = int(time)
  19.     time_strings = []
  20.     units = [(int(365.25*24*60*60),
  21.               lambda years: ngettext("%(n)s year","%(n)s years",years)%{'n':years}),
  22.              (31*24*60*60,
  23.               lambda months: ngettext("%(n)s month","%(n)s months",months)%{'n':months}),
  24.              (7*24*60*60,
  25.               lambda weeks: ngettext("%(n)s week","%(n)s weeks",weeks)%{'n':weeks}),
  26.              (24*60*60,
  27.               lambda days: ngettext("%(n)s day","%(n)s days",days)%{'n':days}),
  28.              (60*60,
  29.               lambda hours: ngettext("%(n)s hour","%(n)s hours",hours)%{'n':hours}),
  30.              (60,
  31.               lambda minutes: ngettext("%(n)s minute","%(n)s minutes",minutes)%{'n':minutes}),
  32.              (1,
  33.               lambda seconds: ngettext("%(n)s second","%(n)s seconds",seconds)%{'n':seconds})]
  34.     for divisor,unit_formatter in units:
  35.         time_covered = time / divisor
  36.         if time_covered:
  37.             if round_at and len(time_strings)+1>=round_at:
  38.                 time_covered = int(round(float(time)/divisor))
  39.                 time_strings.append(unit_formatter(time_covered))
  40.                 break
  41.             else:
  42.                 time_strings.append(unit_formatter(time_covered))
  43.                 time = time - time_covered * divisor
  44.     if friendly and len(time_strings)>2:
  45.         time_stings = time_strings[:2]
  46.     if len(time_strings)>2:
  47.         # Translators... this is a messay way of concatenating
  48.         # lists. In English we do lists this way: 1, 2, 3, 4, 5
  49.         # and 6. This set-up allows for the English system only.
  50.         # You can of course make your language only use commas or
  51.         # ands or spaces or whatever you like by translating both
  52.         # ", " and " and " with the same string.
  53.         return _(" and ").join([_(", ").join(time_strings[0:-1]),time_strings[-1]])
  54.     else:
  55.         return _(" ").join(time_strings)
  56.  
  57. def format_time_compact (tim):
  58.     tim = int(tim)
  59.     hours = tim / (60*60)
  60.     minutes = (tim % (60*60)) / 60
  61.     seconds = ((tim % (60*60)) % 60)
  62.     return "%d:%02d:%02d"%(hours,minutes,seconds)
  63.  
  64. def format_date (tim):
  65.     lt = time.localtime(tim)
  66.     hours = int(time.strftime("%H",lt))
  67.     minutes = int(time.strftime("%M",lt))
  68.     diff = time.time() - tim
  69.     to_yesterday = hours*60*60+minutes*60
  70.     if diff < to_yesterday:
  71.         # then we're today
  72.         # Translators, see strftime manual in order to translate %? format strings
  73.         return time.strftime(_('Today %R %p'),lt)
  74.     elif diff < (to_yesterday + 60*60*24):
  75.         # Translators, see strftime manual in order to translate %? format strings
  76.         return time.strftime(_('Yesterday %R %p'),lt)
  77.     elif diff < (60*60*24*7): # less than a week
  78.         # Translators, see strftime manual in order to translate %? format strings
  79.         return time.strftime(_("%A %H:%M"),lt) # Day, Hour:Minutes
  80.     else:
  81.         # Translators, see strftime manual in order to translate %? format strings
  82.         return time.strftime(_("%A %B %d %R %p"),lt)
  83.  
  84. def format_friendly_date (tim):
  85.     lt = time.localtime(tim)
  86.     hours = int(time.strftime("%H",lt))
  87.     minutes = int(time.strftime("%M",lt))
  88.     diff = time.time() - tim
  89.     ct = time.localtime(); chour=ct[3]; cmin=ct[4]
  90.     to_yesterday = chour*60*60+cmin*60
  91.     if diff < to_yesterday:
  92.         # Then we're today
  93.         if diff < (60*60): # within the hour...
  94.             if (int(diff) / 60):
  95.                 m = (diff / 60)
  96.                 return ngettext("%(n)s minute ago",
  97.                                 "%(n)s minutes ago",m)%{'n':int(m)}
  98.             else: # within the minute
  99.                 return ngettext("%(n)s second ago",
  100.                                 "%(n)s seconds ago",
  101.                                 int(diff))%{'n':int(diff)}
  102.         else:
  103.             # Translators, see strftime manual in order to translate %? format strings
  104.             return time.strftime(_("at %I:%M %p"),lt)
  105.     elif diff < to_yesterday + (60*60*24):
  106.         # Translators, see strftime manual in order to translate %? format strings
  107.         return time.strftime(_("yesterday at %I:%M %p"),lt)
  108.     elif diff < to_yesterday + (60*60*24)*6:
  109.         # Translators, see strftime manual in order to translate %? format strings
  110.         return time.strftime(_("%A %I:%M %p"),lt)
  111.     else:
  112.         # Translators, see strftime manual in order to translate %? format strings
  113.         return time.strftime(_("%B%e"),lt)
  114.  
  115. class ActiveTimer (gobject.GObject):
  116.     """A timer to keep track of how much time a window is active."""
  117.  
  118.     __gsignals__ = {
  119.         'timing-started':(gobject.SIGNAL_RUN_LAST,gobject.TYPE_NONE,()),
  120.         'timing-stopped':(gobject.SIGNAL_RUN_LAST,gobject.TYPE_NONE,())
  121.         }
  122.     
  123.     def __init__ (self, window):
  124.         gobject.GObject.__init__(self)
  125.         self.window = window
  126.         self.timing_running = False
  127.         self.__absolute_start_time__ = 0
  128.         self.tot_time = 0
  129.         self.tot_time_complete = 0
  130.         self.window.connect('window-state-event',self.window_state_event_cb)
  131.         self.window.connect('state-changed',self.window_state_event_cb)
  132.         self.window.connect('visibility-notify-event',self.window_state_event_cb)
  133.         self.window.connect('expose-event',self.window_state_event_cb)        
  134.         self.window.connect('no-expose-event',self.window_state_event_cb)        
  135.  
  136.     def window_state_event_cb (self, *args):
  137.         if self.window.is_active():
  138.             self.toggle_timing(True)
  139.         else:
  140.             self.toggle_timing(False)
  141.  
  142.     def toggle_timing (self, on):
  143.         if not self.__absolute_start_time__:
  144.             return
  145.  
  146.         if on and not self.timing_running:            
  147.             self.timing_started_at = time.time()
  148.             self.timing_running = True
  149.             self.emit('timing-started')
  150.  
  151.         if not on and self.timing_running:
  152.             end_time = time.time()
  153.             self.timing_running = False
  154.             self.tot_time += (end_time - self.timing_started_at)
  155.             self.tot_time_complete += end_time - self.__absolute_start_time__
  156.             self.emit('timing-stopped')
  157.  
  158.     def start_timing (self):
  159.         self.timing_running = False
  160.         self.__absolute_start_time__ = 0
  161.         self.tot_time = 0
  162.         self.tot_time_complete = 0
  163.         self.__absolute_start_time__ = time.time()
  164.         self.toggle_timing(True)
  165.  
  166.     def finish_timing (self):
  167.         self.toggle_timing(False)
  168.         if self.tot_time < 1:
  169.             self.tot_time = 1;
  170.         # dirty hack: never let total time be less than active time
  171.         if self.tot_time > self.tot_time_complete:
  172.             self.tot_time_complete = self.tot_time;
  173.  
  174.     # make sure to call finish_timing before using this function
  175.     def active_time_string (self):
  176.         return format_time(self.tot_time)
  177.     
  178.     # make sure to call finish_timing before using this function
  179.     def total_time_string (self):
  180.         return format_time(self.tot_time_complete)
  181.  
  182. if gtk.pygtk_version[1]<8: gobject.type_register(ActiveTimer)
  183.  
  184.